package com.ktjy.springboot_ch02;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.springboot_ch02.entity.SysUser;
import com.ktjy.springboot_ch02.mapper.SysUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@AutoConfigureMockMvc
public class SysUserMapperTester {

    @Autowired
    private SysUserMapper sysUserMapper;


    @Test
    public void testPagination(){
        QueryWrapper<SysUser> wrapper=new QueryWrapper<SysUser>();
        wrapper.eq("roleId",2L);

        Page<SysUser> page=new Page<SysUser>(1,2);
        IPage<SysUser> userIPage=sysUserMapper.selectPage(page,wrapper);
        System.out.println("总记录数："+userIPage.getTotal());
        System.out.println("总页数："+userIPage.getPages());
        System.out.println("当前页："+userIPage.getCurrent());
        System.out.println("每页记录数："+userIPage.getSize());
        System.out.println("当前记录：");
        for (SysUser user:userIPage.getRecords()){
            System.out.println("userId："+user.getId());
        }
    }

    @Test
    public void testUpdateAndVersion(){
        SysUser sysUser=sysUserMapper.selectById(1L);
        sysUser.setPassword("123456");
        sysUser.setRoleId(1L);

        int result=sysUserMapper.updateById(sysUser);
        if (result==0){
            System.out.println("更新失败，数据可能已经被其他用户修改");
        }else {
            System.out.println("更新成功");
        }
    }

//    @Test
//    public void testFullUpdate(){
//        SysUser sysUser=new SysUser();
//        sysUser.setId(16L);
//        sysUser.setPassword("11111111111");
//    }

    @Test
    public void testDeleteByLogic(){
        sysUserMapper.deleteById(15L);
    }

    @Test
    public void  testUpdate(){
        SysUser sysUser=new SysUser();
        sysUser.setPassword("111111111111");
        sysUser.setId(15L);

        sysUserMapper.updateById(sysUser);
    }
}
